package org.van.carfiauth.service.user;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.van.carfi.common.kernel.constant.ErrorConst;
import org.van.carfi.common.kernel.constant.StatusConst;
import org.van.carfi.common.kernel.constant.UserGenderConst;
import org.van.carfiauth.dao.UserDao;
import org.van.carfiauth.exception.BusinessException;
import org.van.carfiauth.model.UserDomain;
import org.van.carfiauth.utils.TaleUtils;

import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserDomain login(String userAccount, String password) {

        if (StringUtils.isBlank(userAccount) || StringUtils.isBlank(password)) {
            throw new BusinessException(ErrorConst.Auth.USER_ACCOUNT_PASSWORD_IS_EMPTY);
        }

        String pwd = TaleUtils.MD5encode(userAccount + password);
        UserDomain user = userDao.getUserInfoByCond(userAccount, pwd);
        if (null == user) {
            throw new BusinessException(ErrorConst.Auth.USER_ACCOUNT_PASSWORD_ERROR);
        }
        return user;
    }

    @Override
    public UserDomain getUserInfoById(Integer uid) {
        this.userIdExist(uid);
        return userDao.getUserInfoById(uid);
    }

    @Override
    public UserDomain getUserBriefInfoById(Integer uid) {
        this.userIdExist(uid);
        return userDao.getUserBriefInfoById(uid);
    }

    @Override
    public void register(UserDomain user){

        if (StringUtils.isBlank(user.getUacc()) || StringUtils.isBlank(user.getUpassword())) {
            throw new BusinessException(ErrorConst.Auth.USER_ACCOUNT_PASSWORD_IS_EMPTY);
        }
        String pwd = TaleUtils.MD5encode(user.getUacc() + user.getUpassword());
        user.setUpassword(pwd);

        if (Objects.isNull(user.getUgender())) user.setUgender(UserGenderConst.UNDETERMINED);
        if (Objects.isNull(user.getUstatus())) user.setUstatus(StatusConst.User.NORMAL);
        if (Objects.isNull(user.getUname())) user.setUname(user.getUacc());

        // 检查用户账号不存在
        this.userAccountNotExist(user.getUacc());

        userDao.saveUserInfo(user);
    }

    @Override
    public void userStatusNormalByUid(Integer uid) {
        // 检查用户是否存在
        UserDomain userExist = userDao.getUserInfoById(uid);
        if (userExist == null) {
            throw new BusinessException(ErrorConst.Auth.USER_NOT_EXIST);
        }
        // 检查用户状态是否正常
        if (!userExist.getUstatus().equals(StatusConst.User.NORMAL)) {
            throw new BusinessException(ErrorConst.Auth.USER_STATUS_NOT_NORMAL);
        }
    }

    @Override
    public void userIdExist(Integer uid) {
        UserDomain userExist = userDao.getUserInfoById(uid);
        if (userExist == null) {
            throw new BusinessException(ErrorConst.Auth.USER_NOT_EXIST);
        }
    }

    @Override
    public void userAccountNotExist(String userAccount) {
        UserDomain userExist = userDao.getUserInfoByUacc(userAccount);
        if (userExist != null) {
            throw new BusinessException(ErrorConst.Auth.USER_ACCOUNT_EXIST);
        }
    }

}